﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GenericLibrariesToolKit;
using GenericLibrariesToolKit.Enumeration;
using Model;
using BLL.Users.Impl;
using BLL.Users;
namespace BLL.Privilege.Impl
{
    public class RoleModelRightImpl : DaoSupport<RoleRight>, RoleModelRight
    {

        /// <summary>
        /// 存放用户对各模块的访问权限
        /// </summary>
        public static Dictionary<string, Dictionary<string, int>> roleModelRight=new Dictionary<string,Dictionary<string,int>> ();

        /// <summary>
        /// 得到用户的角色权限
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public bool GetRoleRight()
        {
            //设置order by 排序
            Dictionary<string, string> orderBy = new Dictionary<string,string>();
            orderBy.Add("RoleId", "asc");
            orderBy.Add("ModelId", "asc");
            //根据条件获取实体记录List集合
            QueryResult<RoleRight> roleRight = this.GetScrollData(-1, -1, null, orderBy);
            //模块权限求和,并将模块ID和权限之和作为键值对存入modelRight
            Dictionary<string, int>[] modelRight = new Dictionary<string, int>[]
            {
                 new Dictionary<string, int>(),
                 new Dictionary<string, int>(),
                 new Dictionary<string, int>(),
            };
            int sumRight=0;
            int index = 0;
            int roleNumber = 0;
            for (; index < roleRight.TotalRecord; index++)
            {
                if (index == 0 || roleRight.ResultList[index].RoleId == roleRight.ResultList[index - 1].RoleId)
                {
                    if (index == 0 || roleRight.ResultList[index].ModelId == roleRight.ResultList[index - 1].ModelId)
                    {
                        sumRight += roleRight.ResultList[index].RoleRights;
                    }
                    else
                    {
                        modelRight[roleNumber].Add(roleRight.ResultList[index - 1].ModelId, sumRight);
                        sumRight = 0;
                        sumRight = roleRight.ResultList[index].RoleRights;
                    }
                }
                else
                {
                    modelRight[roleNumber].Add(roleRight.ResultList[index - 1].ModelId, sumRight);
                    roleModelRight.Add(roleRight.ResultList[index - 1].RoleId, modelRight[roleNumber]);
                    sumRight = 0;
                    sumRight = roleRight.ResultList[index].RoleRights;
                    roleNumber++;
                }
            }
            modelRight[roleNumber].Add(roleRight.ResultList[index-1].ModelId, sumRight);
            //将用户模块权限存入到静态属性roleModelRight中
            roleModelRight.Add(roleRight.ResultList[index-1].RoleId, modelRight[roleNumber]);
            return true;
        }
        
    }
}
